<template>
  <div>
    <!--头部输入框和按钮-->
    <div>
      <el-input
          size="small"
          class="addPostInput"
          placeholder="添加职位"
          suffix-icon="el-icon-plus"
          @keydown.enter.native="addPosition"
          v-model="pos.name">
      </el-input>

      <el-button @click="addPosition" size="small" icon="el-icon-plus" type="primary">添加</el-button>
    </div>
    <!--表格-->
    <div class="posManaMain">
      <el-table
          size="small"
          stripe
          border
          :data="positions"
          @selection-change="handleSelectionChange"
          style="width: 70%">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createDate"
            width="150"
            label="创建时间">
        </el-table-column>
        <el-table-column
            prop="enabled"
            width="200"
            label="是否启用">
          <template v-slot:default="scope">
            <el-switch
                style="margin-left: 6px"
                v-model="scope.row.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                @change="onEnabled($event, scope.row)"
                inactive-text="未启用">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--批量删除功能按钮-->
    <el-button @click="batchDelete" :disabled="multipleSelection.length === 0" size="small" style="margin-top: 8px" type="danger">批量删除</el-button>

    <!--修改对话框-->
    <el-dialog
        title="编辑职位"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <el-tag>职位名称</el-tag>
        <el-input size="small" v-model="updatePos.name" class="updatePosInput"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="onUpdate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: "PosMana",
  data(){
    return {
      pos: {
        name: ''
      },
      positions: [],
      dialogVisible: false,
      updatePos: {
        name: ''
      },
      multipleSelection: []
    }
  },
  methods: {
    //初始化数据
    initPositions(){
      this.getRequest('/system/basic/pos/').then(res => {
        if (res){
          this.positions = res
        }
      })
    },
    //添加
    addPosition(){
      if (this.pos.name){
        this.postRequest('/system/basic/pos/', this.pos).then(res => {
          if (res){
            //添加成功刷新数据
            this.initPositions()
            //清空文本框
            this.pos.name = ''
          }
        })
      }else {
       this.$message.error('职位名称不能为空！')
      }
    },
    showEditView(index, row){
      //这里直接赋值会有数据绑定，修改输入框，表格数据也会修改，要改为复制属性
      //this.updatePos = row
      //复制属性解决上面的bug
      Object.assign(this.updatePos, row)
      //将日期值置为空，由后端来处理日期,否则修改会报未知错误
      this.updatePos.createDate = ''
      this.dialogVisible = true
    },
    //修改
    onUpdate(){
      this.putRequest('/system/basic/pos/', this.updatePos).then(res => {
        if (res) {
          //刷新数据
          this.initPositions()
          //隐藏对话框
          this.dialogVisible = false
        }
      })
    },
    onEnabled(state, row){
      //将日期值置为空，由后端来处理日期,否则修改会报未知错误
      row.createDate = ''
      this.putRequest('/system/basic/pos/', row).then(res => {
        if (res) {
          //刷新数据
          this.initPositions()
        }
      })
    },
    //删除
    handleDelete(index, row){
      this.$confirm('此操作将永久删除：'+ row.name +', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/pos/' + row.id).then(res => {
          if (res){
            //删除后刷新数据
            this.initPositions()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //多选框改变事件
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    //批量删除
    batchDelete(){
      this.$confirm('此操作将永久删除：'+ this.multipleSelection.length +'条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids ='?'
        this.multipleSelection.forEach(item => ids += 'ids=' + item.id + '&')
        this.deleteRequest('/system/basic/pos/' + ids).then(res => {
          if (res){
            //批量删除后刷新数据
            this.initPositions()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  mounted() {
    this.initPositions()
  }
}
</script>

<style scoped>
.addPostInput {
  width: 300px;
  margin-right: 8px;
}
.posManaMain {
  margin-top: 10px;
}
.updatePosInput {
  width: 200px;
  margin-left: 8px;
}

</style>
